package com.fresher.service;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import com.fresher.dao.UserDao;
import com.fresher.model.UserRole;

@Service
public class UserService {
	
	@Autowired
	MessageSource messageSource;
	
	@Autowired
	private UserDao userDao;

	// create transaction
	public String changeUserPassword(String loginId, String curentPassword,
			String oldPassword, String newPassword, String confirmPassword) {
		Locale locale = LocaleContextHolder.getLocale();
		String message = "";
		if (oldPassword.equals(curentPassword)) {
			if (confirmPassword.equals(newPassword)) {
				userDao.updateUserPassword(loginId, newPassword);
//				message = "Password changed succsessful!";
				message = messageSource.getMessage("label.passwordChanged", null, locale);
			} else {
//				message = "ConfirmPassword and NewPassword are not matched!";
				message = messageSource.getMessage("label.passNotMatch", null, locale);
			}
		} else {
//			message = "Password is incorrect!";
			message = messageSource.getMessage("label.wrongPassword", null, locale);
		}
		return message;

	}

	public String getLoggedUserName() {
		Authentication auth = SecurityContextHolder.getContext()
				.getAuthentication();
		if (!(auth instanceof AnonymousAuthenticationToken)) {
			UserDetails userDetail = (UserDetails) auth.getPrincipal();
			return userDetail.getUsername();
		}
		return null;
	}

	// create new user
	public String createNewuser(String userName, String passWord,
			String userRole) {
		Locale locale = LocaleContextHolder.getLocale();
		String mess = null;
		boolean addScuccess;
		if (userName.length() > 6 || userName.length()==0 
				|| passWord.length()==0) {
//			mess = "Invalid username or password!";
			mess = messageSource.getMessage("label.invalidUserPass", null, locale);
		} else {
			if (null == userRole || userRole.equals("ROLE_ADMIN")) {
				addScuccess=userDao.createNewUser(userName, getEncodedSha1Sum(passWord),
						userRole);
				if (addScuccess==true) {
//					mess = "User added!";
					mess = messageSource.getMessage("label.userAdded", null, locale);
				} else {
//					mess = "Something went wrong!";
					mess = messageSource.getMessage("label.sthError", null, locale);
				}
				
				
			} else {
//				mess = "Invalid user role!";
				mess = messageSource.getMessage("label.roleError", null, locale);
			}
		}
		return mess;
	}

	// get user role list
	public List<UserRole> getUserRoleList() {
		List<UserRole> listRole = userDao.getUserRole();
		return listRole;
	}

	// check username exist
	public boolean checkUsername(String username) {
		boolean exist = true;
		exist = userDao.getUser(username);
		return exist;
	}
	//hash SHA1
	public String getEncodedSha1Sum(String key) {

		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA1");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		md.update(key.getBytes());
		//hexadecimal
		return new BigInteger(1, md.digest()).toString(16);

	}
}
